/**
* Returns an XMLHttp instance to use for asynchronous
* downloading. This method will never throw an exception, but will
* return NULL if the browser does not support XmlHttp for any reason.   
* @return {XMLHttpRequest|Null}
*/
function createXmlHttpRequest() {
  try {
    if (typeof ActiveXObject != 'undefined') {
      return new ActiveXObject('Microsoft.XMLHTTP');       
    } else if (window["XMLHttpRequest"]) {
      return new XMLHttpRequest();                       
    }
  } catch (e) {
    changeStatus(e);    
  }
  return null;          
};    

/**
* This function downloads a text file from a remote URL (from 
* the boat)
*/
function downloadRemoteUrl(i, url, callback) {
  // DEBUG: The remote URL of the text/XML status file to be read
  //alert(url);     
  $.post("lib/io/getFile.php",      
      'val='+url,         
      function(data) {
    // DEBUG: Content of text/XML status file
    //alert(data);     
    callback(xmlParse(data), i);    
  });     
}

/**
* This functions wraps XMLHttpRequest open/send function.           
* It lets you specify a URL and will call the callback if
* it gets a status code of 200.     
* @param {String} url The URL to retrieve
* @param {Function} callback The function to call once retrieved.        
*/
function downloadUrl(i, url, callback) {
  var status = -1;        
  var request = createXmlHttpRequest();      
  if (!request) {
    return false;       
  } 
 
  //alert("URL " + url);   
  request.overrideMimeType('text/xml');   
  request.onreadystatechange = function() {
    //alert("in on change");    
    if (request.readyState == 4) {
      //alert("hi");   
      try {
        status = request.status;    
      } catch (e) {
        // Usually indicates request timed out in FF.           
      }
      //alert(request.responseText);    
      if (status == 200) {
        //alert(request.responseText);    
        if(url.indexOf(".txt")) {
          
          //alert(request.responseText);    
          callback(xmlParse(request.responseText), i, request.status);    
          //alert("text " + url);          
        } else {
          //callback(request.responseXML, i, request.status);       
          //alert("xml " + url);    
        }
        request.onreadystatechange = function() {};           
      }
    }
  }
  request.open('GET', url, true);         
  try {
    request.send(null);      
  } catch (e) {
    changeStatus(e);        
  }
  
  //var txt = request.responseText;    
  //alert(txt);       

};      

/**
 * Parses the given XML string and returns the parsed document in a
 * DOM data structure. This function will return an empty DOM node if
 * XML parsing is not supported in this browser.
 * @param {string} str XML string.
 * @return {Element|Document} DOM.
 */
function xmlParse(str) {
  //alert("In parser");
  if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
    var doc = new ActiveXObject('Microsoft.XMLDOM');
    doc.loadXML(str);
    return doc;
  }

  if (typeof DOMParser != 'undefined') {
    return (new DOMParser()).parseFromString(str, 'text/xml');
  }
  return createElement('div', null);
}

/**
 * Appends a JavaScript file to the page.
 * @param {string} url
 */
function downloadScript(url) {
  var script = document.createElement('script');
  script.src = url;
  document.body.appendChild(script);
}
